package cn.edu.bupt.mybatis.controller;

import cn.edu.bupt.mybatis.entity.Comment;
import cn.edu.bupt.mybatis.entity.Product;
import cn.edu.bupt.mybatis.mapper.CommentMapper;
import cn.edu.bupt.mybatis.mapper.ProductMapper;

import cn.edu.bupt.mybatis.vo.ProductWithComments;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/product")
public class ProductController {

    @Resource
    ProductMapper productMapper;
    @Resource
    CommentMapper commentMapper;

    @GetMapping(path = "")
    public List<?> listAll() {
        List<Product> products = productMapper.findAll();
        return products;
    }

    //获取所有商品及所有评论列表,嵌套查询
    @GetMapping(path = "/all/comment/all")
    public List<?> listAllWithComments() {
        List<Map> productWithComments = productMapper.findAllWithComments();
        return productWithComments;
    }

    //获取所有商品及所有评论列表V2,映射嵌套结果集合
    @GetMapping(path = "/all/comment/all/v2")
    public List<?> listAllWithCommentsV2() {
        List<ProductWithComments> productWithComments = productMapper.findAllWithCommentsV2();
        return productWithComments;
    }

    //获取商品评论列表
    @GetMapping(path = "/{prodId}/comment/")
    public List<?> commentListAll(@PathVariable int prodId) {
        List<Comment> comments = commentMapper.findByProdId(prodId);
        return comments;
    }


}
